package com.rk.jarjuggler.model;

public class LibNode extends DirNode {
    private static final long serialVersionUID = 1L;
    
    private String jarUrl;
    private String javadocUrl;
    private String srcUrl;

    public LibNode(DirNode parent, String name) {
        super(parent, name);
    }

    public LibNode(DirNode parent, String name, String jarUrl) {
        super(parent, name);
        this.jarUrl = jarUrl;
    }

    public boolean isLeaf() {
        return true;
    }

    public String getJarUrl() {
        return jarUrl;
    }

    public void setJarUrl(String newJarUrl) {
        if (getJarUrl() != null) {
            System.out.println("+++++ Jar url already set");
//            throw new IllegalStateException("Jar url already set");
        }

        System.out.println("newJarUrl = " + newJarUrl);
        this.jarUrl = newJarUrl;
    }

    public boolean hasJar(){
        return jarUrl != null;
    }

    public String getJavadocUrl() {
        return javadocUrl;
    }

    public void setJavadocUrl(String newJavadocUrl) {
        if (getJavadocUrl() != null) {
            System.out.println("+++++ Javadoc url already set");
//            throw new IllegalStateException("Javadoc url already set");
        }
        System.out.println("newJavadocUrl = " + newJavadocUrl);
        this.javadocUrl = newJavadocUrl;
    }

    public boolean hasJavadoc(){
        return javadocUrl != null;
    }

    public String getSrcUrl() {
        return srcUrl;
    }

    public void setSrcUrl(String newSrcUrl) {
        if (getSrcUrl() != null) {
            System.out.println("+++++ Src url already set");
//            throw new IllegalStateException("Src url already set");
        }

        System.out.println("newSrcUrl = " + newSrcUrl);
        this.srcUrl = newSrcUrl;
    }

    public boolean hasSrc(){
        return srcUrl != null;
    }

    public int compareTo(DirNode dirNode) {
        //inverse the sort order for leaf nodes
        return -1 * super.compareTo(dirNode);
    }

    public boolean passesFilter(String searchText, boolean hasJar, boolean hasSrc, boolean hasJavadoc) {

        boolean containsLibs = containsLibs(hasJar, hasSrc, hasJavadoc);
        if (searchText == null || searchText.length() == 0){
            return containsLibs;
        }

        String upperSearchText = searchText.toUpperCase();
        return containsLibs && getDirParent().getName().toUpperCase().contains(upperSearchText);
    }

    private boolean containsLibs(boolean hasJar, boolean hasSrc, boolean hasJavadoc) {
        return (!hasJar || (hasJar() == hasJar)) && (!hasSrc || (hasSrc() == hasSrc)) && (!hasJavadoc || (hasJavadoc() == hasJavadoc));
    }
}
